package com.water.util;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.water.devtool.Context;
import com.watermud.util.StringUtil;

public class StringGenerater {
	final static Logger log = LoggerFactory.getLogger(StringGenerater.class);

	public static String get(String ori, Context context) {
		GenericTokenParser parser = new GenericTokenParser("{#", "}", new GenericTokenParser.TokenHandler() {

			@Override
			public String handleToken(String content) {
				if (StringUtils.isBlank(content)) {
					return "";
				}
				String key = null;
				String function = null;
				if (content.indexOf("?") > 0) {
					String[] temp = content.split("\\?");
					key = temp[0];
					function = temp[1];
				} else {
					key = content;
				}
				Object temp = context.get(key);
				if (temp == null) {
					return key;
				}
				String value = temp.toString();
				if (StringUtils.isNotBlank(function)) {
					value = dealString(value, function);
				}

				log.debug("转换[{}]为[{}]", new String[] { content, value });
				return value;
			}
		});
		String result = parser.parse(ori);
		log.debug("{}转换结果为：{}", ori, result);
		return result;

	}

	protected static String dealString(String value, String function) {
		String reuslt = value;
		switch (function) {
		case "caption":
			reuslt = StringUtil.capitalize(value);
			break;
		case "uncaption":
			reuslt = StringUtil.unCapitalize(value);
			break;
		case "upper":
			reuslt = value.toUpperCase();
			break;
		case "lower":
			reuslt = value.toLowerCase();
			break;
		case "uncap_first":
			reuslt = StringUtils.uncapitalize(value);
			break;
		default:
			break;
		}
		return reuslt;
	}

	public static void main(String[] args) {
		StringGenerater.get("com.wckj.{#project}.{#modular}.{#classlevel}", new Context());
	}
}
